Page({
  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var a = res.windowHeight;
        that.setData({
          scrollTop: a - 200
        })
      }
    }),
    this.setData({
      btn_data : false
    })
      this.redata()

  },

  onReady: function (e) {
    
  },
  data: {
    pic: "/icon/paly.png",
    btn_data :false,
    musiclist: [],
    searchValue: '',　　　　　　//搜索值
    musiclrc: {},
    mid: 0,
    lrctext: '',
    twolrc: "",
    scrollTop: 0,
    music: {
      pic: "https:\/\/api.mlwei.com\/music\/api\/?key=523077333&cache=1&type=pic&id=003xQWR33oVCI1",

      author: '',
      title: '',
      url: 'https:\/\/api.mlwei.com\/music\/api\/?key=523077333&type=url&id=003xQWR33oVCI1&size=hq',
      lrc: ""

    }
  },
  redata : function(){
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    wx.showToast({
      title: '正在加载',
      icon: 'loading',
      duration: 10000000,
      mask: true
    })
    this.audioCtx = wx.createAudioContext('myAudio');
    var that = this;
    wx.request({
      url: 'https://api.mlwei.com/music/api/?key=523077333&id=3641614987&type=songlist&cache=1&size=hq',
      method: 'GET',
      success: function (res) {
        console.log(res.data);
        var a = new Array();
        for (var i = 0; i < res.data.Body.length; i++) {
          a.push(res.data.Body[i])
          if (i == 200) {
            break;
          }
        }
        that.setData({
          musiclist: a

        })
        wx.hideToast();
      }
    })
  },
  audioPlay: function () {

    var that = this;
    wx.showToast({
      title: '拼命加载中',
      icon: 'loading',
      duration: 10000000,
      mask: true
    })
    if (that.data.pic == "/icon/paly.png") {
      console.log(this.audioCtx);
      showlrc(that, '');
      this.audioCtx.play()
      that.setData({
        pic: "/icon/pause.png"
      })

    } else {
      this.audioCtx.pause()
      that.setData({
        pic: "/icon/paly.png"
      })
    }
    wx.hideToast();

  },
  searchValueInput: function (e) {//得到搜索框的内容并渲染到data下的searchValue中
    var value = e.detail.value;
    this.setData({
      searchValue: value,
    });
  },

  //点击列表播放音乐
  musictap: function (event) {
    wx.showToast({
      title: '拼命加载中',
      icon: 'loading',
      duration: 10000000,
      mask: true
    })
    console.log(event.currentTarget.dataset.cid);
    var idx = event.currentTarget.dataset.cid;
    var lrc = event.currentTarget.dataset.id;

    var that = this;
    that.setData({
      music: that.data.musiclist[idx],
      pic: "/icon/pause.png",
      mid: idx
    })
    wx.hideToast();
    console.log("88888888" + this.data.musiclist[idx].url);
    showlrc(that, this.data.music.lrc);


  },
  musicend: function () {

    var that = this;
    var s = this.data.mid + 1;
    if (s >= this.data.musiclist.length) {
      s = 0
    }
    that.setData({
      music: that.data.musiclist[s],
      pic: "/icon/pause.png",
      mid: s,
      twolrc: that.data.musiclist[s].lrc
    })

    showlrc(that, this.data.twolrc);
  },
  lrcshow: function (event) {
    var that = this;
    var s = parseInt(event.detail.currentTime).toString();
    console.log(this.data.musiclrc[s]);
    that.setData({

      lrctext: this.data.musiclrc[s]

    })

  },

  //按钮点击事件
  btn_search: function () {
    var that = this;
    var count = 0;
    if (this.data.searchValue != '') {
      wx.request({
        url: 'https://api.mlwei.com/music/api/?key=523077333&id=' + this.data.searchValue + '&type=so&cache=0&size=hq',
        method: 'GET',
        success: function (res) {

          wx.showToast({
            title: '正在搜索歌曲',
            icon: 'loading',
            duration: 10000000,
            mask: true
          })
          var b = new Array();
          for (var i = 0; i < res.data.Body.length; i++) {
            var c = res.data.Body[i].url.toString()
            
            if (c.substring(0, 32) == "https://api.mlwei.com/music/api/") {
              console.log(res.data.Body[i].url.toString())
              b.push(res.data.Body[i])
              
            }

          }

          that.setData({
            musiclist: b,
            btn_data :true

          })
          wx.hideToast()

        }

      })
    }
    this.redata()
  },
  btn_head : function(){
    this.redata()
    this.setData({
      btn_data : false
    })
  }
})


function parseLyric(lrc) {
  var lyrics = lrc.split("\n");
  var lrcObj = {};
  for (var i = 0; i < lyrics.length; i++) {
    var lyric = decodeURIComponent(lyrics[i]);
    var timeReg = /\[\d*:\d*((\.|\:)\d*)*\]/g;
    var timeRegExpArr = lyric.match(timeReg);
    if (!timeRegExpArr) continue;
    var clause = lyric.replace(timeReg, '');
    for (var k = 0, h = timeRegExpArr.length; k < h; k++) {
      var t = timeRegExpArr[k];
      var min = Number(String(t.match(/\[\d*/i)).slice(1)),
        sec = Number(String(t.match(/\:\d*/i)).slice(1));
      var time = min * 60 + sec;
      lrcObj[time] = clause;
    }
  }
  return lrcObj;
}

function showlrc(that, lrc) {
  //歌词请求
  if (lrc == '') {
    lrc = "https://api.mlwei.com/music/api/?key=523077333&cache=1&type=lrc&id=003xQWR33oVCI1"
  }
  wx.request({

    url: lrc,
    method: 'GET',
    success: function (res) {
      console.log(res.data);
      var lyric = parseLyric(res.data);

      that.setData({
        musiclrc: lyric
      })
      setTimeout(function () {

        that.audioCtx.play();
      }, 5000)

    }
  })

}



